//
//  HttpService.m
//  EaseMall
//
//  Created by LLMPro on 16/8/23.
//  Copyright © 2016年 LLMPro. All rights reserved.
//

#import "HttpService.h"
#import "AFNetworking.h"
#import "Reachability.h"


@implementation HttpService

+ (HttpService *)shardManager{

    static HttpService * httpService = nil;
    
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        
        httpService = [[HttpService alloc] init];
        
    });
    
    httpService.responseSerializer = [AFHTTPResponseSerializer serializer];
    
    httpService.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html",@"text/css",@"text/plain", nil];
    
    
    return httpService;

}


+ (void)getRequest:(NSString *)url params:(NSDictionary *)params success:(requestSuccessBlock)successHandler failure:(requestFailureBlock)failureHandler{
    
    if (![self checkNetworkStatus]) {
        
        successHandler(nil);
        
        failureHandler(nil);
        
        return;
        
    }
    
    [[self shardManager] GET:url parameters:params success:^(NSURLSessionDataTask *task, id responseObject) {
        
        if (responseObject == nil) {
            
            //数据请求失败
            NSError *error = [[NSError alloc]initWithDomain:@"网络请求数据为空!" code:9999 userInfo:nil];
            
            failureHandler(error);
        }
        
        
        id object = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
        
        successHandler(object);

     
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        
        failureHandler(error);
        
    }];


}

+ (void)postRequest:(NSString *)url params:(NSDictionary *)params success:(requestSuccessBlock)successHandler failure:(requestFailureBlock)failureHandler{
    
    if (![self checkNetworkStatus]) {
        
        successHandler(nil);
        
        failureHandler(nil);
        
        return;
        
    }

    
    [[self shardManager] POST:url parameters:params success:^(NSURLSessionDataTask *task, id responseObject) {
        
        id object = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
        
        successHandler(object);
        
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        
        failureHandler(error);
        
    }];
    

}


+ (BOOL)checkNetworkStatus{
    
    BOOL isConnect;
    
    Reachability * r = [Reachability reachabilityWithHostName:@"www.apple.com"];
    
    NSLog(@"-----====----%ld",(long)[r currentReachabilityStatus]);
    
    if ([r currentReachabilityStatus] == 0) {
        
        isConnect = NO;
        
    }else{
    
        isConnect = YES;
        
        if([r currentReachabilityStatus] == ReachableViaWiFi){
            
            NSLog(@"wifi---------------!");
        }else if([r currentReachabilityStatus] == ReachableViaWWAN){
            
            NSLog(@"2G/3G/4G---------------!");
            
        }
        

        
    }
    
    
    return isConnect;

}



@end
